package cn.com.lms.business.exam.persistence.repository;

import cn.com.lms.business.exam.persistence.entity.Exercise;

import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Repository
public interface ExerciseRepository extends BaseRepository<Exercise,Long> {


    @Transactional
    @Modifying
    @Query(value = "update bnz_exam_exercise set is_delete =:isDelete WHERE id in (:collection)", nativeQuery = true)
    int updateIsDelete(@Param("isDelete") Boolean isDelete , @Param("collection") Collection<Long> collection) ;

    @Transactional
    @Modifying
    @Query(value = "update bnz_exam_exercise set is_enable =:isEnable WHERE id in (:collection)", nativeQuery = true)
    int updateIsEnable(@Param("isEnable") Boolean isEnable , @Param("collection") Collection<Long> collection) ;



    @Query(value = "select tem.* from (select em.employee_id user_id,em.exercise_id as id, ex.exercise_name as name , 2 type,ex.create_time, (select count(1) from bnz_exam_employee_exercise_subject es where es.exercise_id=em.exercise_id) finish from bnz_exam_exercise_employee em LEFT JOIN bnz_exam_exercise ex on em.exercise_id=ex.id where  ex.id is not null UNION All  select ls.user_id,ls.plan_id as id ,lp.plan_name as name ,1 type,lp.create_time,finish_state finish  from  bnz_learning_student ls LEFT JOIN bnz_learning_stage_detail sd on ls.stage_detail_id=sd.id LEFT JOIN bnz_learning_stage st on st.id=sd.stage_id LEFT JOIN bnz_learning_plan lp on lp.id=ls.plan_id " +
            "where ls.finish_state=0  GROUP BY ls.plan_id) tem where tem.user_id=?1 order tem.create_time desc ", nativeQuery = true)
    List<Map> selectToDisposeList(Long userId);



}
